//
// Created by jack on 2020/8/13.
//
#include "wl_osal.h"
#include "wl_types.h"
#include "wlsdk.h"

extern unsigned char burst_tone_offline[2131];

void test_wlsdk_message_callback(wl_uint32 msgid, wl_int32 resultcode, void* msgdata, wl_uint32 datalen) {
    WLLOGD("msgid %d resultcode %d datalen %d", msgid, resultcode, datalen);
    switch (msgid) {
        case WLSDK_MSG_SYSTEM_EXCEPTION:  // 系统异常
            WLLOGD("WLSDK_MSG_SYSTEM_EXCEPTION");
            break;
        case WLSDK_MSG_SYSTEM_SERVER_STATE:  // 服务器连接状态
            WLLOGD("WLSDK_MSG_SYSTEM_SERVER_STATE %d", *((wl_uint32*)msgdata));
            break;

        // login
        case WLSDK_MSG_LOGIN_LOGIN:  // 登录响应
            WLLOGD("WLSDK_MSG_LOGIN_LOGIN");
            wlsdk_get_server_state();
            wlsdk_get_login_state();
            wlsdk_get_selfinfo();
            wlsdk_friend_get_friends();
            wlsdk_group_get_groups();
            break;
        case WLSDK_MSG_LOGIN_LOGOUT:  // 注销响应
            WLLOGD("WLSDK_MSG_LOGIN_LOGOUT");
            break;
        case WLSDK_MSG_LOGIN_STATE:  // 登录状态
            WLLOGD("WLSDK_MSG_LOGIN_STATE %d", *((wl_uint32*)msgdata));
            wlsdk_get_extension_state();
            break;
        case WLSDK_MSG_LOGIN_EXTENSION_STATE:  // 分机状态(设备为分机类型时有效)
            WLLOGD("WLSDK_MSG_LOGIN_EXTENSION_STATE %d", *((wl_uint32*)msgdata));
            break;
        case WLSDK_MSG_LOGIN_EXTENSION_VERCODE:  // 分机绑定验证码
            WLLOGD("WLSDK_MSG_LOGIN_EXTENSION_VERCODE verifycode %s", (wl_char*)msgdata);
            break;
        case WLSDK_MSG_LOGIN_BIND_EXTENSION:  // 绑定分机请求
        {
            wlsdk_extension_bind* extension_bind = (wlsdk_extension_bind*)msgdata;
            WLLOGD("WLSDK_MSG_LOGIN_BIND_EXTENSION userid %d nick %s", extension_bind->userid, extension_bind->nick);
            wlsdk_answer_bind_extension(1);
            // wlsdk_tts("用户请求绑定");
            break;
        }
        case WLSDK_MSG_LOGIN_ANSWER_BIND_EXTENSION:  // 绑定分机应答
            WLLOGD("WLSDK_MSG_LOGIN_ANSWER_BIND_EXTENSION");
            break;

        // user
        case WLSDK_MSG_USER_SELFINFO: {
            wlsdk_user* userinfo = (wlsdk_user*)msgdata;
            WLLOGD("WLSDK_MSG_USER_SELFINFO id %d nick %s number %s phone %s code %s sex %d status %d created %d",
                   userinfo->userid,
                   userinfo->nick,
                   userinfo->number,
                   userinfo->phone,
                   userinfo->country_code,
                   userinfo->sex,
                   userinfo->status,
                   userinfo->created);
            break;
        }

            // group
        case WLSDK_MSG_GROUP_GET_GROUPS: {
            WLLOGD("WLSDK_MSG_GROUP_GET_GROUPS");
            wlsdk_group_list* group_list = (wlsdk_group_list*)msgdata;
            if (group_list) {
                wl_uint32 pos = 0;
                for (pos = 0; pos < group_list->count; pos++) {
                    WLLOGD("group id %d version %d name %s number %s type %d burst type %d owner %d shutupstatus %d "
                           "shieldstatus %d tts %d locationshare %d mversion %d mcount %d mtype %d mprority %d "
                           "mshutupstatus %d mshutuptimeout %d",
                           group_list->groups[pos].groupid,
                           group_list->groups[pos].group_version,
                           group_list->groups[pos].group_name,
                           group_list->groups[pos].group_number,
                           group_list->groups[pos].group_type,
                           group_list->groups[pos].bursttype,
                           group_list->groups[pos].ownerid,
                           group_list->groups[pos].shutupstatus,
                           group_list->groups[pos].shieldstatus,
                           group_list->groups[pos].tts,
                           group_list->groups[pos].locationshare,
                           group_list->groups[pos].member_version,
                           group_list->groups[pos].member_count,
                           group_list->groups[pos].member_type,
                           group_list->groups[pos].member_prority,
                           group_list->groups[pos].member_shutupstatus,
                           group_list->groups[pos].member_shutuptimeout);
                }
            }

            break;
        }

        // friend
        case WLSDK_MSG_FRIEND_GET_FRIENDS: {
            WLLOGD("WLSDK_MSG_FRIEND_GET_FRIENDS");
            wlsdk_friend_list* friend_list = (wlsdk_friend_list*)msgdata;
            if (friend_list) {
                wl_uint32 pos = 0;
                for (pos = 0; pos < friend_list->count; pos++) {
                    WLLOGD("WLSDK_MSG_FRIEND_GET_FRIENDS user id %d number %s nick %s shieldstatus %d tts %d "
                           "locationshare %d",
                           friend_list->friends[pos].userid,
                           friend_list->friends[pos].number,
                           friend_list->friends[pos].nick,
                           friend_list->friends[pos].shieldstatus,
                           friend_list->friends[pos].tts,
                           friend_list->friends[pos].locationshare);
                }
            }

            break;
        }

        // session
        case WLSDK_MSG_SESSION_GET_SESSIONS: {
            wlsdk_session_list* session_list = (wlsdk_session_list*)msgdata;
            if (session_list) {
                wl_uint32 pos = 0;
                // WLLOGD("WLSDK_MSG_SESSION_GET_SESSIONS list count %d attach_max %d",
                //        session_list->count,
                //        session_list->attach_max);
                for (pos = 0; pos < session_list->count; pos++) {
                    WLLOGD("WLSDK_MSG_SESSION_GET_SESSIONS session id %lld type %d name %s isdefault %d active id %lld",
                           session_list->sessions[pos].sessionid,
                           session_list->sessions[pos].sessiontype,
                           session_list->sessions[pos].name,
                           session_list->sessions[pos].isdefault,
                           session_list->active_sessionid);
                    // 只主叫单呼
                    // if (session_list->sessions[pos].sessiontype == WLSDK_SESSION_TYPE_SINGLE) {
                    //     wlsdk_set_burst_session(session_list->sessions[pos].sessionid,
                    //                             session_list->sessions[pos].sessiontype);
                    //     break;
                    // }
                }
                // if (session_list->count > 0) {
                //     wlsdk_set_burst_session(session_list->sessions[pos - 1].sessionid,
                //                             session_list->sessions[pos - 1].sessiontype);
                // }
            }
            break;
        }
        case WLSDK_MSG_SESSION_BURST_SESSION: {
            wlsdk_session* session = (wlsdk_session*)msgdata;
            WLLOGD("WLSDK_MSG_SESSION_BURST_SESSION sessionid %lld type %d name %s timeout %d isdefault %d online %d",
                   session->sessionid,
                   session->sessiontype,
                   session->name,
                   session->timeout,
                   session->isdefault,
                   session->online_count);
            // wlsdk_get_sessions();
            break;
        }

        case WLSDK_MSG_SESSION_SET_BURST_SESSION:
            WLLOGD("WLSDK_MSG_SESSION_SET_BURST_SESSION");
            break;
        case WLSDK_MSG_SESSION_BURST_REQUEST:
            WLLOGD("WLSDK_MSG_SESSION_BURST_REQUEST");
            break;
        case WLSDK_MSG_SESSION_BURST_RELEASE:
            WLLOGD("WLSDK_MSG_SESSION_BURST_RELEASE");
            break;
        case WLSDK_MSG_SESSION_BURST_STATE: {
            wlsdk_session_burst* burst = (wlsdk_session_burst*)msgdata;
            WLLOGW("WLSDK_MSG_SESSION_BURST_STATE session %lld isdefault %d timeout %d burststate %d playtime %d "
                   "duration %d",
                   burst->session.sessionid,
                   burst->session.isdefault,
                   burst->session.timeout,
                   burst->burststate,
                   burst->playtime,
                   burst->duration);
            break;
        }

        case WLSDK_MSG_SESSION_PLAYBACK:
            WLLOGD("WLSDK_MSG_SESSION_PLAYBACK");
            break;

        // location
        case WLSDK_MSG_LOCATION_REQUEST_REALTIME:
        case WLSDK_MSG_LOCATION_REQUEST_TRACK:
        case WLSDK_MSG_LOCATION_REQUEST_SOS: {
            wlsdk_location location;
            location.latitude  = 22.67151387511997;
            location.longitude = 115.0653212994686;
            location.speed     = 1.1;
            location.altitude  = 2.2;
            location.radius    = 3.3;
            location.timestamp = wl_utctime();
            location.direction = 4.4;
            location.type      = 0;
            wlsdk_set_gps_location(&location);

            // 22.67151387511997","longitude":"114.0653212994686

            // wl_char content[] = "20200323000341,V;,,,,,,,,;0;4,1,460,02,9360,4082,131,"
            //                     "9360,4092,148,9360,4091,143,9360,4153,141;1,tplink-ap, 1c:fa:68:13:a5:b4,-72";
            // wl_char content[] =
            //     "20211122151136,A;22.577513,N,113.918475,E,0.05,0.00,-119.00,10,90;0;3,0,460,01,9550,111948034,23,9550,"
            //     "111948035,0,9550,111948036,0;4,,f0:0f:ec:36:f3:f8,-75,,84:d9:31:6d:5b:b0,-90,,84:d9:31:6d:5b:b1,-92,,"
            //     "84:d9:31:6d:5b";
            // wl_char content[] = "20200323000341,V;,,,,,,,,;0;4,1,460,0,10160,9770827,-89,"
            //                     "9360,4092,148,9360,4091,143,9360,4153,141;1,tplink-ap, 1c:fa:68:13:a5:b4,-72";

            // wlsdk_set_yby_location(content);

            // wlsdk_lbs lbs;
            // memset(&lbs, 0, sizeof(wlsdk_lbs));
            // strcpy(lbs.imei, "867924065782545");
            // strcpy(lbs.imsi, "460113448106791");
            // lbs.bts.iscdma            = 0;
            // lbs.bts.bts.mobile.mcc    = 460;
            // lbs.bts.bts.mobile.mnc    = 17;
            // lbs.bts.bts.mobile.lac    = 6226;
            // lbs.bts.bts.mobile.cellid = 239999489;
            // lbs.bts.bts.mobile.signal = -78;
            // wlsdk_set_lbs_location(&lbs);
            WLLOGD("WLSDK_MSG_LOCATION_REQUEST_REALTIME");
            break;
        }
        case WLSDK_MSG_SYSTEM_NEW_VERSION: {
            wlsdk_new_version* version = (wlsdk_new_version*)msgdata;
            if (version) {
                // WLLOGD("version %s", version->version);
            }
            break;
        }
        case WLSDK_MSG_VOIP_INCOMING: {
            wlsdk_session_voip* voip = (wlsdk_session_voip*)msgdata;
            wlsdk_voip_accept();
            break;
        }
        case WLSDK_MSG_USER_CONFIG: {
            if (msgdata) {
                WLLOGD("config %s\n", msgdata);
            }
        }
        default:
            break;
    }
}

void trace_callback(wl_char* buf) {
    printf(buf);
}

int main(int argc, char** argv) {
    wlsdk_init(test_wlsdk_message_callback, trace_callback);
    wl_sleep(1);
    wlsdk_track_config(wl_true, 4);
    // wlsdk_set_appkey("102048", "bd4ae075c24379dbfed5448b74c3cd71");
    // wlsdk_set_hardware("1", "CF01", "865028000000106", "868615030081555", "v1.1", 1, "123", 6);
    // wlsdk_set_login_url("http://webapi.weila.hk/v1/msg-server");
    // wlsdk_set_web_server("webapi.weila.hk", 80);

    // wlsdk_set_appkey("102036", "b3c658bd2e637c65efb134fb381d4a18");
    // wlsdk_set_hardware("1", "VOIS", "868088053592479", "A8686150300815552", "v1.0", 1);  //陈宁账号多群组
    // wlsdk_set_hardware("1", "gw031", "865028000000106", "868615030081555", "v1.1", 1);
    // wlsdk_set_hardware("1", "CF01", "868088053597072", "868615030081555", "v1.1", 1);
    // wlsdk_set_login_url("http://webapi.weila.hk:8080/v1/msg-server-test");
    // wlsdk_set_web_server("webapi.weila.hk", 8080);

    // wlsdk_set_hardware("1", "UNIPRO_ZX", "868615030081555", "868615030081661", "v1.0", 1);
    // wlsdk_set_login_url("http://192.168.0.128/v1/msg-server-inner");
    // wlsdk_set_web_server("192.168.0.128", 80);

    wlsdk_set_appkey("102036", "b3c658bd2e637c65efb134fb381d4a18");
    wlsdk_set_hardware("1", "CC01", "868088053592482", "868615030081662", "v1.0", 1, "123", 6);
    wlsdk_set_login_url("http://demo.weila.hk:80/v1/msg-server");
    wlsdk_set_web_server("demo.weila.hk", 80);
    wl_sleep(1);
    wlsdk_set_network(0, 0, WLSDK_SYSTEM_NETWORK_CONNECTED);
    wlsdk_login();
    wl_uint32 sleeptime = 0;
    wl_char   text[100];

    wl_sleep(5);
    // wlsdk_session_playback();
    wl_bool pressed = wl_true;
    wlsdk_set_heartbeat_interval(100);
    wlsdk_check_version();
    wlsdk_battery_update(20);
    wlsdk_alarm_power(32);
    wlsdk_report_reset_modem();
    wlsdk_report_reboot(1341234);
    do {
        wlsdk_get_user_config();
        // wlsdk_open_sos();
        // wlsdk_location location;
        // location.latitude  = 23.12343234;
        // location.longitude = 112.2341341234;
        // location.speed     = 1.1;
        // location.altitude  = 2.2;
        // location.radius    = 3.3;
        // location.timestamp = wl_utctime();
        // location.direction = 4.4;
        // wlsdk_set_lbs_location(&location);
        // wl_sleep(30);
        wlsdk_get_sessions();

        // WLLOGW("sleeptime %d used %d free %d fresh %d",
        //        sleeptime++,
        //        wl_mem_num_used(),
        //        wl_mem_num_free(),
        //        wl_mem_num_fresh());
        sprintf(text, "测试TTS缓存序号是否正确,序号");
        // wlsdk_tts(text);
        // wl_sleep(10);
        // wlsdk_play_audio(burst_tone_offline, sizeof(burst_tone_offline));
        // wl_sleep(1);
        // wlsdk_session_playback();
        // wl_sleep(10);
        // wlsdk_audio_pause(10);
        // wlsdk_session_burst_request();
        wl_sleep(15);
        // wlsdk_battery_update(rand() % 100);
        // wlsdk_open_sos();
        // wlsdk_session_burst_release();
        // wl_sleep(15);
        // wlsdk_check_version();
        // wlsdk_alarm_power(10);

        // wl_sleep(600);

        if (pressed) {
            // wlsdk_session_burst_request();
            // wlsdk_set_network(0, 0, WLSDK_SYSTEM_NETWORK_CONNECTED);
        } else {
            // wlsdk_session_burst_release();
            // wlsdk_set_network(0, 0, WLSDK_SYSTEM_NETWORK_DISCONNECTED);
            // wlsdk_tts(text);
            // wl_sleep(10);
            // wlsdk_play_audio(burst_tone_offline, sizeof(burst_tone_offline));
            // wl_sleep(1);
            // wlsdk_session_playback();
        }
        // wlsdk_get_sessions();
        // wlsdk_session_playback();
        pressed = !pressed;
    } while (wl_true);
    return 0;
}